package com.sunday.scoremanager.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.entity.Member;
import com.sunday.scoremanager.http.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.scoremanager.base.Constants.CODE_5;
import static com.sunday.scoremanager.base.Constants.SEND_CODE_SIGN;

/**
 * Created by 刘涛 on 2017/11/7.
 */

public class SettingPayPwdActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.mobile)
    TextView mobile;
    @Bind(R.id.edit_code)
    ClearEditText editCode;
    @Bind(R.id.send_code)
    TextView sendCode;
    @Bind(R.id.edit_pwd)
    ClearEditText editPwd;
    @Bind(R.id.edit_confirm_pwd)
    ClearEditText editConfirmPwd;

    private int type;//
    private String mobi;
    private TimeCount timeCount;
    private boolean fromWithDraw;
    private Member member;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pay_pwd);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", 0);
        fromWithDraw = getIntent().getBooleanExtra("fromWithDraw", false);
        initView();

    }

    private void initView() {
        member = BaseApp.getInstance().getMember();
        mobi = BaseApp.getInstance().getMember().getLinkPhone();
        mobile.setText(mobi);

        switch (type) {
            case 0:
                //设置
                titleView.setText("设置交易密码");
                break;
            case 1:
                //修改
                titleView.setText("修改交易密码");
                break;
            case 2:
                //忘记
                titleView.setText("忘记交易密码");
                break;
        }
        timeCount = new TimeCount(60000, 1000, sendCode);
    }

    private String pwd, pwd1, code;

    @OnClick({R.id.send_code, R.id.btn_confirm})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_code:
                if (!StringUtils.isMobileNO(mobi)){
                   ToastUtils.showToast(mContext,"手机号码不正确");
                }else {
                    sendCode();
                }
                break;
            case R.id.btn_confirm:
                code = editCode.getText().toString().trim();
                pwd = editPwd.getText().toString().trim();
                pwd1 = editConfirmPwd.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    ToastUtils.showToast(mContext, "请输入验证码");
                    return;
                }


                if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd1)) {
                    ToastUtils.showToast(mContext, "请输入密码");
                    return;
                }

                if(pwd.length()!=6){
                    ToastUtils.showToast(mContext, "请输入6位数密码");
                    return;
                }
                if (!pwd.equals(pwd1)) {
                    ToastUtils.showToast(mContext, "两次密码输入不一致");
                    return;
                }

                updatePwd();

                break;
        }
    }

    private void sendCode() {
        Call<ResultDO> call = ApiClient.getApiAdapter().sendCode(mobi, SEND_CODE_SIGN, CODE_5, 5);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "验证码已发送到手机");
                    timeCount.start();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    private void updatePwd() {
        Call<ResultDO<Member>> call = ApiClient.getApiAdapter().setTradePwd(2, member.getSellerId(), pwd, code);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                ResultDO<Member> resultDO = response.body();
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "设置成功");
                    BaseApp.getInstance().setMember(resultDO.getResult());
                    SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
                    if (fromWithDraw) {
                        Intent data = new Intent();
                        setResult(RESULT_OK, data);
                        finish();
                    } else finish();
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount != null) {
            timeCount.cancel();
        }
    }

}